iT邦幫忙

2023 iThome 鐵人賽

DAY 26
0
Mobile Development

好好用的 flutter 套件系列 第 26

好好用的 flutter 套件 - Day 26 firebase_auth

  • 分享至 

  • xImage
  •  

需求

firebase 提供簡單好用的認證註方式,如果沒server的話,這個很好用呀!!

firebase_auth - Readme

Firebase 控制台 - Authentication

 Authentication 選擇認證的方式,這裏選擇用匿名方式,比較不會顯示個資,
 大家可以依自行認證方式,來作選擇。
 

https://ithelp.ithome.com.tw/upload/images/20231010/20121643bi1u83qyuq.png

https://ithelp.ithome.com.tw/upload/images/20231010/20121643X60gWeRpu3.png

firebase_auth - Install

直接在 pubspec.yaml 加上 firebase_auth: ^4.10.1,然後pub get 
dependencies:
  firebase_auth: ^4.10.1

https://ithelp.ithome.com.tw/upload/images/20231010/20121643EUlEGusH1f.png

firebase_auth - Example

在 /lib/main.dart 加入 程式
import 'package:firebase_auth/firebase_auth.dart';
宣告和初始化

  late final FirebaseApp app;
  late final FirebaseAuth auth;
  
  Future<void> firebaseAuth() async {
    WidgetsFlutterBinding.ensureInitialized();
    // We're using the manual installation on non-web platforms since Google sign in plugin doesn't yet support Dart initialization.
    // See related issue: https://github.com/flutter/flutter/issues/96391

    // We store the app and auth to make testing with a named instance easier.
    app = await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
    auth = FirebaseAuth.instanceFor(app: app);

    if (shouldUseFirebaseEmulator) {
      await auth.useAuthEmulator('localhost', 9099);
    }
    _showToast(auth.toString());
    //runApp(const AuthExampleApp());
  }

Future<void> firebaselogin() async {
    try {
      final UserCredential userCredential = await auth.signInAnonymously();
      final User? user = userCredential.user;
      //day23 fluttertoast
      _showToast(user.toString());
      //day9 Logger
      logger.d("firebaseAuth ${user.toString()}");
      print("firebaselogin "+user.toString());
    } on FirebaseAuthException catch (e) {
      log("firebaseAuth "+e.toString());
      _showToast(e.toString());
    } catch (e) {
      log("firebaseAuth "+e.toString());
      _showToast(e.toString());
    }
  }

void initState() {
    firebaseAuth() 
}
    ElevatedButton(
      onPressed: firebaselogin,
      child: const Text('Day26 Firebase Auth'),
    ),

執行結果

https://ithelp.ithome.com.tw/upload/images/20231010/201216430grOJkjdng.png

https://ithelp.ithome.com.tw/upload/images/20231010/201216434oAcx40EDW.png

完整的log
firebaseAuth User(displayName: , email: , isEmailVerified: false, isAnonymous: true, metadata: UserMetadata(creationTime: 2023-10-10 06:01:37.203Z, lastSignInTime: 2023-10-10 06:01:37.203Z), phoneNumber: , photoURL: null, providerData, [], refreshToken: null, tenantId: null, uid: 0h8li89Nj6Tffk6BgmjlKBK1zpe2)

firebase 網頁
https://ithelp.ithome.com.tw/upload/images/20231010/20121643fQzYgJBVoa.png

心得

firebase_auth 用起來真的簡單方便,這裏是因為demo用匿名的方式,
還有其他的方式大家可以去用哦!!

參考

https://firebase.google.com/docs/auth/flutter/start?hl=zh-tw


上一篇
好好用的 flutter 套件 - Day 25 firebase_core
下一篇
好好用的 flutter 套件 - Day 27 firebase_remote_config
系列文
好好用的 flutter 套件30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言